package api

import (
	"cap/dao"
	"cap/service"
	"github.com/gin-gonic/gin"
	"lib/http"
	"lib/id"
	"lib/structs/cap"
	"lib/structs/tables"
	"time"
)

var HostApi = new(hostApi)

type hostApi struct {
}

func (a hostApi) AddHost(c *gin.Context) {
	var in cap.AddHostForm
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	me := http.GetCurrentUser(c)
	err := service.HostSer.AddHost(in, me)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a hostApi) PageHost(c *gin.Context) {
	var in cap.SearchHostForm
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	li, total, err := service.HostSer.FindPage(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OkPage(c, li, total)
}
func (a hostApi) DelHost(c *gin.Context) {
	in := struct {
		Id int64 `form:"id" binding:"required"`
	}{}
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	me := http.GetCurrentUser(c)
	err := service.HostSer.Delete(in.Id, me)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a hostApi) AddCluster(c *gin.Context) {
	var in cap.AddClusterForm
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	me := http.GetCurrentUser(c)
	err := service.HostSer.AddCluster(in, me)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a hostApi) EditCluster(c *gin.Context) {
	var in cap.AddClusterForm
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	me := http.GetCurrentUser(c)
	err := service.HostSer.UpdateCluster(in, me)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a hostApi) FindClusterList(c *gin.Context) {
	li, err := service.HostSer.FindClusterList()
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, li)
}
func (a hostApi) FindClusterDtoList(c *gin.Context) {
	li, err := service.HostSer.FindClusterDtoList()
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, li)
}
func (a hostApi) DelCluster(c *gin.Context) {
	in := struct {
		Id int64 `form:"id" binding:"required"`
	}{}
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	me := http.GetCurrentUser(c)
	err := service.HostSer.DelCluster(in.Id, me)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a hostApi) LinkBindUser(c *gin.Context) {
	var in cap.AddLinkDto
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.HostSer.LinkBindUser(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a hostApi) LinkList(c *gin.Context) {
	me := http.GetCurrentUser(c)
	li, err := service.HostSer.LinkList(*me)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, li)
}

func (a hostApi) LinkDetail(c *gin.Context) {
	in := struct {
		Id int64 `form:"id" binding:"required"`
	}{}
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	m, err := service.HostSer.GetLinkDetail(in.Id)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, m)
}

func (a hostApi) GetCertifyList(c *gin.Context) {
	me := http.GetCurrentUser(c)
	li, err := service.HostSer.GetCertifyList(*me)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, li)
}

func (a hostApi) CertifyPage(c *gin.Context) {
	var in cap.SearchCertifyForm
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	li, total, err := service.HostSer.CertifyPage(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OkPage(c, li, total)
}

func (a hostApi) DelCertify(c *gin.Context) {
	in := struct {
		Id int64 `form:"id" binding:"required"`
	}{}
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := dao.DB.Model(&tables.CapCertify{}).Where("id = ?", in.Id).Delete(&tables.CapCertify{}).Error
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a hostApi) AddCertify(c *gin.Context) {
	var in cap.AddCertifyForm
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	me := http.GetCurrentUser(c)
	m := tables.CapCertify{
		Id:          id.GetSnowId(),
		Type:        in.Type,
		SourceId:    0,
		Account:     in.Account,
		Password:    in.Password,
		Description: in.Description,
		Creator:     me.Nickname,
		CreatorId:   me.UserID,
		CreatedAt:   time.Now().Unix(),
	}
	err := dao.DB.Model(&tables.CapCertify{}).Create(&m).Error
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a hostApi) BindCluster(c *gin.Context) {
	in := struct {
		HostId    int64 `json:"hostId,string" binding:"required"`
		ClusterId int64 `json:"clusterId,string" binding:"required"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.HostSer.BindCluster(in.HostId, in.ClusterId)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a hostApi) AppList(c *gin.Context) {
	li, err := service.HostSer.AppList()
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.Ok(c, li)
}
func (a hostApi) AppIssue(c *gin.Context) {
	var in cap.AppIssueForm
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.HostSer.AppIssue(in)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}

func (a hostApi) NoticeHostUploadApps(c *gin.Context) {
	in := struct {
		Id int64 `json:"id,string" binding:"required"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	if err := service.HostSer.NoticeHostUploadApps(in.Id); err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
func (a hostApi) NoticeHostRegister(c *gin.Context) {
	in := struct {
		Id int64 `json:"id,string" binding:"required"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	if err := service.HostSer.NoticeHostRegister(in.Id); err != nil {
		http.BadWithErr(c, err)
		return
	}
	http.OK204(c)
}
